{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--BOOK_INFORMATION-->\n",
    "<img align=\"left\" style=\"padding-right:10px;\" src=\"figures/PHydro-cover-small.png\">\n",
    "*This is the Jupyter notebook version of the [Python in Hydrology](http://www.greenteapress.com/pythonhydro/pythonhydro.html) by Sat Kumar Tomer.*\n",
    "*Source code is available at [code.google.com](https://code.google.com/archive/p/python-in-hydrology/source).*\n",
    "\n",
    "*The book is available under the [GNU Free Documentation License](http://www.gnu.org/copyleft/fdl.html). If you have comments, corrections or suggestions, please send email to satkumartomer@gmail.com.*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--NAVIGATION-->\n",
    "< [Writing Raster](06.03-Writing-Raster.ipynb) | [Contents](Index.ipynb) | [Reading the Raster](06.05-Reading-the-Raster.ipynb) >"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6.4 写入矢量\n",
    "\n",
    "shap文件(.shp)格式是相当常用的矢量数据类型。让我们写入一个shapefile。要写shapefile，我们将使用`ogr`包。OGR是GDAL库的一部分。OGR处理矢量格式，而GDAL主库则用于栅格格式。在`http://www.gdal.org/ogr/ogr_formats.html`上给出了OGR支持的格式列表以及创建驱动程序时使用的代码名称。让我们说，我们想制作一个具有四个城市位置和名称的shapefile。城市的细节如下：\n",
    "\n",
    "|||\n",
    "|---|:---:|\n",
    "|Name|Latitude|Longitude|\n",
    "|Bijnor|29.4|78.1\n",
    "|Delhi|28.6|77.2\n",
    "|Bangalore|13.0|77.8\n",
    "|Berambadi|11.8|76.6\n",
    "\n",
    "我们从导入`ogr`库和定义城市的坐标及名称开始。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ogr\n",
    "lat = [29.4,28.6,13.0,11.8]\n",
    "lon = [78.1,77.2,77.8,76.6]\n",
    "name = ['Bijnor', 'Delhi', 'Bangalore', 'Berambadi']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "现在，我们定义了驱动程序的名称(ESRI Shapefile)，并创建数据源。`driver.CrateDataSource`定义将保存数据的文件夹的名称。`ds.CreateLayer`定义shapefile的名称及几何类型(在这种情况下指的是点)。然后我们定义字段名为`Name`，并说它是一个最大宽度为16的字符串类型。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "driver = ogr.GetDriverByName(\"ESRI Shapefile\")\n",
    "ds = driver.CreateDataSource('datas/location')\n",
    "layer = ds.CreateLayer('location', geom_type=ogr.wkbPoint)\n",
    "field_defn = ogr.FieldDefn('Name', ogr.OFTString )\n",
    "field_defn.SetWidth(16)\n",
    "\n",
    "layer.CreateField(field_defn)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "现在，我们准备好基本信息了，我们可以开始添加关于城市的信息(名称和位置)。首先我们创建一个特征(feature)来存储城市的信息。然后，我们在`Name`字段内添加城市的名称。在此之后，我们说它是一个点类型，并且我们添加它的经度和维度。最终，我们销毁特征和数据源，这样就不必做其他的事情了，我们的数据也保存得很好。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "i = 0\n",
    "for i in range(len(name)):\n",
    "    feature = ogr.Feature(layer.GetLayerDefn())\n",
    "    feature.SetField('Name', name[i])\n",
    "    pt = ogr.Geometry(ogr.wkbPoint)\n",
    "    pt.SetPoint_2D(0, lon[i], lat[i])\n",
    "    feature.SetGeometry(pt)\n",
    "    layer.CreateFeature(feature)\n",
    "    feature.Destroy()\n",
    "ds.Destroy()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们可以在任何GIS查看器中看到这个shapefile。如图6.5显示了使用QGGS生成的城市的位置。\n",
    "\n",
    "![location](figures/location.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<center>图6.5:城市的位置</center>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
